package com.wfk.service.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.image.common.RestUploadFileInfo;
import com.wfk.constant.PathConstant;
import com.wfk.entity.Goods;
import com.wfk.entity.GoodsSpec;
import com.wfk.entity.SysService;
import com.wfk.mapper.GoodsMapper;
import com.wfk.service.IGoodsService;
import com.wfk.service.IGoodsSpecService;
import com.wfk.service.ISysServiceService;
import com.wfk.utils.BasePage;
import com.wfk.utils.FunctionUtils;
import com.wfk.utils.ResponseData;
import com.wfk.utils.ValidateUtil;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @since 2019-01-12
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
	@Autowired
	private ISysServiceService sysServiceService;
	@Autowired
	private IGoodsSpecService goodsSpecService;
	@Override
	public BasePage<Goods> getList(Goods goods, BasePage<Goods> page) {
		QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("createtime");
		if(goods.getStatus()!=null) {
			queryWrapper.eq("status",goods);
		}else {
			queryWrapper.eq("status",1);
		}
		super.page(page, queryWrapper);
		return page;
	}

	@Override
	public Goods getGoodsDetail(String goodsCode) {
		Goods goods = super.getById(goodsCode);
		if(goods!=null) {
			goods.setSysService(sysServiceService.list(new QueryWrapper<SysService>().eq("parentid", goods.getServiceId())));
			goods.setGoodsSpec(goodsSpecService.list(new QueryWrapper<GoodsSpec>().eq("parent",goods.getSpecid())));
		}
		return goods;
	}

	@Override
	public ResponseData insertGoods(Goods goods,MultipartFile pfile, MultipartFile[] ufile) throws IOException {
			if(!ValidateUtil.empty(goods.getGoodsCode())){
				return ResponseData.baseError("请输入商品编号");
			}else if(!ValidateUtil.empty(goods.getTitle())){
				return ResponseData.baseError("请输入商品名称");
			}else if(ValidateUtil.objEmpty(goods.getServiceId())){
				return ResponseData.baseError("请选择商品服务");
			}else if(!ValidateUtil.empty(goods.getImgDetail())){
				return ResponseData.baseError("请编辑详情");
			}else if(!ValidateUtil.empty(goods.getGoodsDetail())){
				return ResponseData.baseError("请编辑商品参数");
			}else if(ValidateUtil.objEmpty(goods.getSpecid())){
				return ResponseData.baseError("请选择商品规则");
			}
			
			 if(this.sysServiceService.getById(goods.getServiceId())==null){
				return ResponseData.baseError("商品方服务不存在，请刷新页面重新选择");
			}
			if(ufile!=null&&ufile.length>0) {
				StringBuffer buffer = new StringBuffer();
				for(MultipartFile file:ufile) {
					RestUploadFileInfo upload = com.image.common.Service.uploadImage(file.getOriginalFilename(), file.getInputStream(), PathConstant.goods);
					if(upload.isStatus()) {
						buffer.append(upload.getServiceName() + upload.getFilePath() + upload.getFileName()).append(",");
					}
				}
				if(buffer.length()>0) {
					buffer.delete(buffer.length()-1, buffer.length());
				}
				goods.setUrl(buffer.toString());
			}
			if(pfile!=null) {
				RestUploadFileInfo upload = com.image.common.Service.uploadImage(pfile.getOriginalFilename(), pfile.getInputStream(), PathConstant.goods);
				if(upload.isStatus()) {
					goods.setPurl(upload.getServiceName() + upload.getFilePath() + upload.getFileName());
				}
			}
			
			boolean save = super.saveOrUpdate(goods);
			if(save==true){
				return ResponseData.ok("上传成功");
			}
			return ResponseData.baseError("上传失败");
	}

	@Override
	public Map<String, Goods> listMap() {
		List<Goods> list = super.list(null);
		Map<String, Goods>  map = new HashMap<>();
		list.forEach( v->{
			Random random = new Random() ;
			int num = random.nextInt(5) - 3;
			System.out.println(num);
			BigDecimal dayProfit = v.getDayProfit();
			dayProfit = FunctionUtils.add(new BigDecimal(num), dayProfit, 2) ; 
			v.setDayProfit(dayProfit);
			map.put(v.getGoodsCode(), v);
		});
		return map;
	}

}
